package com.ourteam.workspace.api;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ourteam.frame.web.AbstractTreeBuilder;
import com.ourteam.frame.web.ITreeNodeBuilder;
import com.ourteam.frame.web.TreeViewNodeBean;

public class WorkspaceTreeBuilder extends AbstractTreeBuilder {

	private File workspaceRootDir;
	
	private long workSpaceId;
	
	private static Map<String, String> nodeActionMap = new HashMap<String, String>();

	static {
		nodeActionMap.put("folderNode", "workspaceFolderNodeActionGroup");
		nodeActionMap.put("workspaceSourceCodeNode", "workspaceResourceFolderNodeActionGroup");
		nodeActionMap.put("workspaceWebNode", "workspaceResourceFolderNodeActionGroup");
		nodeActionMap.put("workspaceWebPageNode", "workspaceResourceFolderNodeActionGroup");
		nodeActionMap.put("workspaceLibaryNode", "workspaceResourceFolderNodeActionGroup");
	}

	public WorkspaceTreeBuilder(File workspaceRootDir,long workSpaceId) {
		super();
		this.workspaceRootDir = workspaceRootDir;
		this.workSpaceId = workSpaceId;
	}

	@Override
	protected String getTreeConfigFile() {
		return "classpath:ui/workspace/workspace_tree.xml";
	}
	
	protected boolean isShowTag(){
		return true;
	}
	
	@Override
	protected TreeViewNodeBean configTreeViewNodeProperties(Object obj, TreeViewNodeBean treeViewNodeBean) {

		String actionGroupId = nodeActionMap.get(treeViewNodeBean.getNodeConfigId());

		if (StringUtils.isNotBlank(actionGroupId)) {
			treeViewNodeBean.setActionGroupId(actionGroupId);
		}

		return treeViewNodeBean;
	}

	@Override
	protected void initTreeNodeBuilder(ITreeNodeBuilder treeNodeBuilder) throws Exception {
		if (treeNodeBuilder instanceof IWorkspaceResourceNodeBuilder) {
			((IWorkspaceResourceNodeBuilder) treeNodeBuilder).setWorkspaceRootDir(workspaceRootDir);
		}
		if (treeNodeBuilder instanceof WorkspaceNodeBuilder) {
			((WorkspaceNodeBuilder) treeNodeBuilder).setWorkspaceId(workSpaceId);
		}
		

	}

}
